import {DOMHelper} from "../Utility/DOMHelper.js";
import {Tooltip} from "./Tooltip.js";

export class ProjectItem{
    hasActiveTooltip = false;
    constructor(id,updateProjectListsFunction,type) {
        this.id = id;
        this.updateProjectListsHandler =  updateProjectListsFunction;
        this.connectMoreInfoButton();
        this.connectSwitchButton(type);
    }

    showMoreInfoHandler(){
        if(this.hasActiveTooltip){
            return;
        }
        const projectElement = document.getElementById(this.id);
        const tooltipText = projectElement.dataset.extraInfo;

        const tooltip = new Tooltip(()=>{
                this.hasActiveTooltip = false;
            },
            tooltipText,
            this.id);

        tooltip.attach();
        this.hasActiveTooltip = true;
    }

    connectMoreInfoButton(){
        const projectItemElement = document.getElementById(this.id);
        let moreInfoBtn = projectItemElement.querySelector('button:first-of-type');
        moreInfoBtn.addEventListener('click',this.showMoreInfoHandler.bind(this));
    };

    connectSwitchButton(type){
        const projectItemElement = document.getElementById(this.id);
        let switchBtn = projectItemElement.querySelector('button:last-of-type');
        switchBtn = DOMHelper.clearEventListeners(switchBtn);
        switchBtn.textContent = type === 'active' ? 'Finish' : 'Activate';
        switchBtn.addEventListener(
            'click',this.updateProjectListsHandler.bind(null,this.id));
    };

    update(updateProjectListsFn,type){
        this.updateProjectListsHandler = updateProjectListsFn;
        this.connectSwitchButton(type);
    }
}
